package edu.gatech.oad.antlab.person;

import java.util.ArrayList;
import java.util.HashSet;

/**
 *  A simple class for person 2
 *  returns their name and a
 *  modified string 
 *
 * @author Bob
 * @version 1.1
 */
public class Person2 {
    /** Holds the persons real name */
    private String name;
	 	/**
	 * The constructor, takes in the persons
	 * name
	 * @param pname the person's real name
	 */
	 public Person2(String pname) {
	   name = pname;
	 }
	/**
	 * This method should take the string
	 * input and return its characters in
	 * random order.
	 * given "gtg123b" it should return
	 * something like "g3tb1g2".
	 *
	 * @param input the string to be modified
	 * @return the modified string
	 */
	private String calc(String input) {	
		int length = input.length();

		// Will store the indices of characters we have already
		// used from the original string
		HashSet<Integer> usedIndices = new HashSet<Integer>();
		
		String result = "";
		int randIndex = (int)(Math.random() * length);
		for (int i = 0; i < length; i++) {
			// Keep looking till we find a letter that hasn't been used yet
			while (usedIndices.contains(randIndex)) {
				randIndex = (int)(Math.random() * length);
			}
			usedIndices.add(randIndex);
			
			// Now put that letter in the new string
			result += input.charAt(randIndex);
		}
		
		// At this point we have used all letters in the original string
		// and put them into random places in the new string
		return result;
	}
	
	/**
	 * Return a string rep of this object
	 * that varies with an input string
	 *
	 * @param input the varying string
	 * @return the string representing the 
	 *         object
	 */
	public String toString(String input) {
	  return name + calc(input);
	}
}
